package com.ycc.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ycc.model.entity.Result;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * 拦截controller返回值，封装后统一返回格式
 */
@RestControllerAdvice
@Slf4j
public class ResponseAdvice implements ResponseBodyAdvice<Object> {
 
    @Autowired
    private ObjectMapper objectMapper;
 
    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        // 返回true，表示拦截Controller所有方法的返回值
        return true;
    }
 
    @SneakyThrows
    @Override
    public Object beforeBodyWrite(Object o, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        //如果Controller返回String的话，SpringBoot不会帮我们自动封装而直接返回，因此我们需要手动转换成json。
        if (o instanceof String) {
            return objectMapper.writeValueAsString(Result.ok(o));
        }
        //如果返回的结果是R对象，即已经封装好的，直接返回即可。
        //如果不进行这个判断，后面进行全局异常处理时会出现错误
        if (o instanceof Result) {
            return o;
        }
        log.debug("result : {}", objectMapper.writeValueAsString(Result.ok(o)));
        return Result.ok(o);
    }
}
 